//25 clock
int redled = 1;
int greedLed = 2;
int m_gpio= 0;
int key= 4;

int  m_count = 0;
int timemin = 0;
int timesec = 0;
int val = 0;
int AlarmTime = 40;
int c_state = 0;
int spk_count = 0;
void myDelay(int time)
{
 while(time --)
 {
    delay(1);
 }
}
void ledBlink(int count)
{
  while(count --)
  {
    digitalWrite(greedLed,HIGH );   // turn the LED on (HIGH is the voltage level)
    digitalWrite(redled, LOW);  
    digitalWrite(m_gpio, HIGH);
    myDelay(100);  
    digitalWrite(greedLed,LOW );    // turn the LED off by making the voltage LOW
    digitalWrite(redled, HIGH);  
    digitalWrite(m_gpio, LOW);
    myDelay(100);  
  }
}
// the setup routine runs once when you press reset:
void setup() {                
  pinMode(redled, OUTPUT);    
  pinMode(greedLed, OUTPUT);  
  pinMode(m_gpio, OUTPUT); 
  pinMode(key, INPUT); 
  digitalWrite(redled, HIGH);  
  digitalWrite(greedLed, HIGH);  
  
  ledBlink(10);
  digitalWrite(redled, HIGH); 
  digitalWrite(greedLed, LOW); 
  digitalWrite(m_gpio, LOW);  
}
// the loop routine runs over and over again forever:
void loop() {
  
  myDelay(500);
 
  timesec ++;
  if(timesec >=120 )
  {
    timesec = 0;
    timemin ++ ;
  }
  //LED 
  if(timemin >= AlarmTime)
  {
    //work
    if(timemin >= AlarmTime)
    {
      AlarmTime = 10;
      digitalWrite(greedLed,HIGH ); 
      digitalWrite(redled,LOW );
      c_state = 1;
      m_count = 0;
    }
    //sleep
    else
    {
      AlarmTime = 40;
      digitalWrite(greedLed,LOW ); 
      digitalWrite(redled,HIGH );
      c_state = 1; 
      m_count = 0;
    }
    timemin = 0;
  }
  
  val = digitalRead(4);
  if(val == HIGH)
  {
    c_state = 0;
    digitalWrite(m_gpio, LOW);
  }
  
 if (c_state != 0) {  
     m_count ++;
     if(spk_count == 1)
     {
      digitalWrite(m_gpio, LOW);  
      spk_count = 0;
     }
     else {
      digitalWrite(m_gpio, HIGH); 
      spk_count = 1;
     }
     //timeoushutdown
     if(m_count >= 120){c_state = 0;m_count = 0;digitalWrite(m_gpio, LOW); spk_count = 0; }
  }
}



